package org.vectrics.recruiter.struts.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Category;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;

public class TravelComboTag extends TagSupport {

	private static final long serialVersionUID = 1705282191763737747L;
	
	static private Category log = Category.getInstance(TravelComboTag.class);
    private String styleClass = null;
    private String property = null;
    private String jobId = null;
    
    public TravelComboTag() {
    	super();
    }


	public int doStartTag() throws JspException  {
		log.debug("doStartTag()");
		StringBuffer html = new StringBuffer();
		long travelPercent = 0;
		
		log.debug("doStartTag() called");
		HttpServletRequest request = (HttpServletRequest)super.pageContext.getRequest();
		String contextPath = request.getContextPath();
		
		if (property == null) {
			throw new JspException("Property attribute not specified.");
		}
		String propValue = request.getParameter(property);
		log.debug("doStartTag() - property = " + property);
		Job job = null;
		if (jobId != null) {
			job = RecruitServiceLocator.getJobProvider().findJob(new Long(jobId));
		}

		if (job == null) {
			log.debug("Could not find job with id");
			travelPercent = 0;
		}
		else {
		    if (job.getTravelPercent() != null)
		        travelPercent = job.getTravelPercent().longValue();
		    else
		        travelPercent = 0;
			travelPercent = (travelPercent / 10) * 10;
		}

		
		//HttpSession session = super.pageContext.getSession();
		if (styleClass == null) {
			html.append("<select name=\"" + property + "\" style=\"width:250;font-family:verdana,arial,sans serif;font-size=" +
					"11px\">");
		}
		else {
			html.append("<select name=\"" + property + "\" class=\"" + styleClass + "\">");
		}
		
		html.append("<option value=\"\"></option>");
		for (int percent = 0; percent <= 100; percent += 10) {
			String selectStr = "";
			if (travelPercent == percent) {
				log.debug("user is the same as property value, this is selected user");
				selectStr = "selected";
			}
			html.append("<option value=\"" + percent + "\"" + selectStr + ">" 
					+ percent + "%</option>");
		}
		
		html.append("</select>");

		
		JspWriter writer = pageContext.getOut();
		try {
			writer.print(html.toString());
		}
		catch (IOException ex) {
			throw new JspException("IOException attempting to write tag to output. " + ex.getMessage());
		}
		return(EVAL_PAGE);
	}


	/**
	 * @return
	 */
	public String getStyleClass() {
		return styleClass;
	}

	/**
	 * @param styleClass
	 */
	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	/**
	 * @return
	 */
	public String getProperty() {
		return property;
	}

	/**
	 * @param property
	 */
	public void setProperty(String property) {
		this.property = property;
	}

	/**
	 * @return
	 */
	public String getJobId() {
		return jobId;
	}

	/**
	 * @param jobId
	 */
	public void setJobId(String jobId) {
		this.jobId = jobId;
	}

}
